package org.ehe.finance.subject.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.finance.subject.domain.bo.ErpFinanceAccountSubjectBo;
import org.ehe.finance.subject.domain.vo.ErpFinanceAccountSubjectTreeVo;
import org.ehe.finance.subject.domain.vo.ErpFinanceAccountSubjectVo;
import org.ehe.finance.subject.service.ErpFinanceAccountSubjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 财务科目类别
 * @author :zhangnn
 * @className :ErpFinanceAccountSubjectController
 * @description: TODO
 * @date 2025-08-22 09:49:45
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/erp/finance/subject")
public class ErpFinanceAccountSubjectController extends BaseController {

    private final ErpFinanceAccountSubjectService financeAccountSubjectService;

    /**
     * 查询批次信息列表
     */
    @GetMapping("/page")
    public TableDataInfo<ErpFinanceAccountSubjectVo> list(ErpFinanceAccountSubjectBo bo, PageQuery pageQuery) {
        return financeAccountSubjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 新增科目
     */
    @Log(title = "财务-新增科目", linkType = "finance", businessType = BusinessType.INSERT, idField = "id", nameField = "subjectName")
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpFinanceAccountSubjectBo bo) {
        return toAjax(financeAccountSubjectService.insertByBo(bo));
    }

    /**
     * 修改科目
     */
    @Log(title = "财务-修改科目", linkType = "finance", businessType = BusinessType.UPDATE, idField = "id", nameField = "subjectName")
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpFinanceAccountSubjectBo bo) {
        return toAjax(financeAccountSubjectService.updateByBo(bo));
    }

    /**
     * 财务-删除科目
     * @param id
     * @return
     */
    @Log(title = "财务-删除科目", linkType = "finance", businessType = BusinessType.UPDATE, idField = "id", nameField = "subjectName")
    @PostMapping("/delete/{id}")
    public R<Void> delete(@PathVariable Long id) {
        return toAjax(financeAccountSubjectService.deleteById(id));
    }


    /**
     * 检查科目编码唯一性
     */
    @GetMapping("/checkCodeUnique")
    public boolean checkCodeUnique(String subjectCode, Long id) {
        return financeAccountSubjectService.checkSubjectCodeUnique(subjectCode, id);
    }

    /**
     * 获取所有科目树形结构
     */
    @GetMapping("/tree")
    public R<List<ErpFinanceAccountSubjectTreeVo>> getAllSubjectTree() {
        return R.ok(financeAccountSubjectService.getAllSubjectTree());
    }

    /**
     * 根据父级ID获取子科目树形结构
     */
    @GetMapping("/tree/{parentId}")
    public R<List<ErpFinanceAccountSubjectTreeVo>> getChildSubjectTree(@PathVariable Long parentId) {
        return R.ok(financeAccountSubjectService.getChildSubjectTree(parentId));
    }
}
